﻿//通用模块
Ctn.util = {
    //遍历操作
    each:function(object, callback, args){
        if (args) {
		    if (object.length == undefined) {
			    for (var name in object)
				    if (callback.apply(object[ name ], args) === false)
					    break;
		    } else
			    for (var i = 0, length = object.length; i < length; i++)
				    if (callback.apply(object[ i ], args) === false)
					    break;
	    } else {
		    if (object.length == undefined) {
			    for (var name in object)
				    if (callback.call(object[ name ], name, object[ name ]) === false)
					    break;
		    } else
			    for (var i = 0, length = object.length, value = object[0]; 
				    i < length && callback.call(value, i, value) !== false; value = object[++i]){}
	    }

	    return object;
    },
    //功能扩展
    extend:function(dest, source, opt){
        if (opt == null || opt == true){
            for (var k in source) {
                dest[k] = source[k];
            }
        }else if (typeof(opt) == "boolean"){
            for (var k in source) {
                if (!(k in dest[k])){
                    dest[k] = source[k];
                }
            }
        }else if(opt instanceof(Array)){
            opt.each(function(k){dest[k] = source[k]});
        }else if(typeof(opt) == "string"){
            var arr = opt.split(',');
            arr.each(function(k){dest[k] = source[k]});
        }
        return dest;
    },
    //遍历尝试
    tryeach:function(){
        var ret;
        for (var i = 0, l = arguments.length; i < l; i++) {
          var fn = arguments[i];
          try {
            ret = fn();
            break;
          } catch (e) {}
        }
        return ret;
    },
    //克隆对象
    clone:function(obj, deep){
        var o = {};
        for (var k in obj){
            o[k] = (deep && typeof(obj[k]) == 'object') ? this.clone(obj[k]) : obj[k];
        }
        return o;
    },
    //空函数
    emptyFn:function(){},
    //是否是函数
    isFunction:function(fn){
        return !!fn && typeof fn != "string" && !fn.nodeName && 
			fn.constructor != Array && /function/i.test( fn + "" );
    }
}